package com.ffcs.web.services;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ffcs.web.daos.implementations.CollectNewsDao;
import com.ffcs.web.daos.implementations.CounselorDao;
import com.ffcs.web.model.entity.CollectNews;
import com.ffcs.web.model.entity.Counselor;
import com.ffcs.web.model.entity.WxUser;

@Service
public class CollectNewsService {

	@Autowired
	private CollectNewsDao collectNewsDao;
	
	@Autowired
	private CounselorDao counselorDao;
	
	@Transactional
	public void updateShouchang(String picNewsId,Integer userId,short type){
		CollectNews collectNews=new CollectNews();
		collectNews.setPicNewsId(Integer.parseInt(picNewsId));
		collectNews.setUserId(userId);
		collectNews.setType(type);
		List<CollectNews> list=collectNewsDao.findByExample(collectNews);
		if(list.size()>0){
			CollectNews collect=list.get(0);
			if(collect.getStatus()==1){
				collect.setStatus((short)0);
			}else{
				collect.setStatus((short)1);
			}
			collectNewsDao.save(collect);
		}else{
			CollectNews collectN=new CollectNews();
			collectN.setAddTime(new Date());
			collectN.setPicNewsId(Integer.parseInt(picNewsId));
			collectN.setUserId(userId);
			collectN.setStatus((short)1);
			collectN.setType(type);
			collectNewsDao.save(collectN);
		}
	}

	public boolean isShouchang(Integer picNewsId, Integer userId,short type) {
		CollectNews collectNews=new CollectNews();
		collectNews.setPicNewsId(picNewsId);
		collectNews.setUserId(userId);
		collectNews.setStatus((short)1);
		collectNews.setType(type);
		List list=collectNewsDao.findByExample(collectNews);
		if(list.size()>0){
			return true;
		}
		return false;
	}

	public List<CollectNews> getMyShouChangList(WxUser wxUser) {
		return collectNewsDao.findByPropertyNameWithOrder("userId",wxUser.getId(), "addTime", false);
	}

	@Transactional
	public void deleteMyShouChang(String id) {
		collectNewsDao.removeById(Integer.parseInt(id));
	}

	@Transactional
	public void updateShouchang(String tel, Integer id) {
		
		Counselor counselor=counselorDao.findUniqueByPropertyName("counseTel",tel);
		CollectNews collectNews=new CollectNews();
		collectNews.setPicNewsId(counselor.getId());
		collectNews.setUserId(id);
		collectNews.setType((short)3);
		List<CollectNews> list=collectNewsDao.findByExample(collectNews);
		if(list.size()>0){
			CollectNews collect=list.get(0);
			if(collect.getStatus()==1){
				collect.setStatus((short)0);
			}else{
				collect.setStatus((short)1);
			}
			collectNewsDao.save(collect);
		}else{
			CollectNews collectN=new CollectNews();
			collectN.setAddTime(new Date());
			collectN.setPicNewsId(counselor.getId());
			collectN.setUserId(id);
			collectN.setStatus((short)1);
			collectN.setType((short)3);
			collectNewsDao.save(collectN);
		}
	}
	
	public boolean isShouchang(String tel, Integer userId) {
		Counselor counselor=counselorDao.findUniqueByPropertyName("counseTel",tel);
		CollectNews collectNews=new CollectNews();
		collectNews.setPicNewsId(counselor.getId());
		collectNews.setUserId(userId);
		collectNews.setStatus((short)1);
		collectNews.setType((short)3);
		List list=collectNewsDao.findByExample(collectNews);
		if(list.size()>0){
			return true;
		}
		return false;
	}
}
